﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FFS.Model;
using FFS.BLL;

namespace FFS
{
    public partial class frmSysParameter : Form
    {
        public frmSysParameter()
        {
            InitializeComponent();
        }

        private string actionString = "";
        SysParameterInfo sysParam = new SysParameterInfo();
        List<SysParameterInfo> sysParamList = new List<SysParameterInfo>();
        SysParamService paramService = new SysParamService();


        private void txt_SysParamName_KeyUp(object sender, KeyEventArgs e)
        {
            this.AcceptButton = this.btn_Save;
        }

        private void txt_SysParamCategory_KeyUp(object sender, KeyEventArgs e)
        {
            this.AcceptButton = this.btn_Save;
        }

        private void cmb_SysParamType_KeyUp(object sender, KeyEventArgs e)
        {
            this.AcceptButton = this.btn_Save;
        }

        private void txt_SysParamRemark_KeyUp(object sender, KeyEventArgs e)
        {
            this.AcceptButton = this.btn_Save;
        }

        private void btn_ClosePanel_Click(object sender, EventArgs e)
        {
            this.panel_editSysParam.Visible = false;
        }

        private void btn_CloseSearch_Click(object sender, EventArgs e)
        {
            this.panel_search.Visible = false;
        }

        private void btn_Save_Click(object sender, EventArgs e)
        {
            string VerifyString = "";
            if (this.txt_SysParamName.Text.Trim() == "")
            {
                VerifyString += "参数名称不能为空\n";
            }
            if (this.cmb_SysParamType.Text.Trim() == "")
            {
                VerifyString += "参数类型不能为空\n";
            }

            if (VerifyString.Trim() != "")
            {
                MessageBox.Show(VerifyString,"提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
            }
            else
            {
                sysParam.SysParamCategory = "";
                sysParam.SysParamName = this.txt_SysParamName.Text.Trim();
                sysParam.SysParamType = this.cmb_SysParamType.SelectedText;
                sysParam.SysParamValue = this.txt_SysParamValue.Text;
                sysParam.SysParamRemark = this.txt_SysParamRemark.Text;
                sysParam.Input_User = Context._Login_UserName;

               try
               {
                    if (actionString == "Add")
                    {
                        paramService.OperageSysParam().CreateSysParam(sysParam);
                        MessageBox.Show("添加成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.txt_SysParamName.Text = "";
                        this.cmb_SysParamType.SelectedIndex = 0;
                        this.txt_SysParamValue.Text = "";
                        this.txt_SysParamRemark.Text = "";
                    }
                    else if (actionString == "Edit")
                    {
                        paramService.OperageSysParam().ModifySysParam(sysParam);
                        MessageBox.Show("编辑成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.panel_editSysParam.Visible = false;
                    }
                    btn_search_Click(sender,e);
               }
               catch (Exception)
               {
                    MessageBox.Show("操作失败","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
               }
            }
        }

        private void tsb_add_Click(object sender, EventArgs e)
        {
            actionString = "Add";
            this.txt_SysParamName.Enabled = true;
            this.txt_SysParamName.Text = "";
            this.cmb_SysParamType.SelectedIndex = 0;
            this.txt_SysParamValue.Text = "";
            this.txt_SysParamRemark.Text = "";
            this.panel_editSysParam.Visible = true;
        }

        private void tsb_edit_Click(object sender, EventArgs e)
        {
            if (this.listview_SysParameterList.SelectedItems.Count == 1)
            {
                actionString = "Edit";
                this.txt_SysParamName.Enabled = false;
                this.panel_editSysParam.Visible = true;
            }
            else
            {
                MessageBox.Show("请选择要编辑的记录行", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            
        }

        private void tsb_delete_Click(object sender, EventArgs e)
        {
            if (listview_SysParameterList.SelectedItems.Count == 1)
            {
                sysParam.SysParamName = this.listview_SysParameterList.SelectedItems[0].SubItems[0].Text;
                sysParam.Input_User = Context._Login_UserName;
                paramService.OperageSysParam().DeleteSysParam(sysParam);
                MessageBox.Show("删除成功","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                this.btn_search_Click(sender,e);
            }
            else
            {
                MessageBox.Show("请选择要删除的记录行", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void tsb_search_Click(object sender, EventArgs e)
        {
            this.panel_search.Visible = !this.panel_search.Visible;
        }

        private void btn_search_Click(object sender, EventArgs e)
        {
            string searchCondition = "";
            if (this.txt_searchContent.Text.Trim() == "")
            {
                BindListView(paramService.OperageSysParam().GetAllSysParam());
            }
            else
            {
                if (this.cmb_searchItem.SelectedIndex == 0)
                {
                    if (this.cmb_search_compare.SelectedIndex == 0)
                    {
                        searchCondition = " SysParamName like N'%" + this.txt_searchContent.Text.Trim() + "%' "; 
                    }
                    else
                    {
                        searchCondition = " SysParamName = N'" + this.txt_searchContent.Text.Trim() + "' ";  
                    }
                }
                else if (this.cmb_searchItem.SelectedIndex == 1)
                {
                    if (this.cmb_search_compare.SelectedIndex == 0)
                    {
                        searchCondition = " SysParamType like N'%" + this.txt_searchContent.Text.Trim() + "%' ";
                    }
                    else
                    {
                        searchCondition = " SysParamType = N'" + this.txt_searchContent.Text.Trim() + "' ";
                    }
                }
                else if (this.cmb_searchItem.SelectedIndex == 2)
                {
                    if (this.cmb_search_compare.SelectedIndex == 0)
                    {
                        searchCondition = " SysParamValue like N'%" + this.txt_searchContent.Text.Trim() + "%' ";
                    }
                    else
                    {
                        searchCondition = " SysParamValue = N'" + this.txt_searchContent.Text.Trim() + "' ";
                    }
                }
                else if (this.cmb_searchItem.SelectedIndex == 3)
                {
                    if (this.cmb_search_compare.SelectedIndex == 0)
                    {
                        searchCondition = " SysParamRemark like N'%" + this.txt_searchContent.Text.Trim() + "%' ";
                    }
                    else
                    {
                        searchCondition = " SysParamRemark = N'" + this.txt_searchContent.Text.Trim() + "' ";
                    }
                }
                BindListView(paramService.OperageSysParam().GetSysParamBySearchCondition(searchCondition));
            }


           
        }

        private void BindListView(List<SysParameterInfo> paramList)
        {
            this.listview_SysParameterList.Items.Clear();
            foreach (SysParameterInfo param in paramList)
            {
                string[] str = new string[4];
                str[0] = param.SysParamName;
                str[1] = param.SysParamType;
                str[2] = param.SysParamValue;
                str[3] = param.SysParamRemark;
                ListViewItem item = new ListViewItem(str);
                this.listview_SysParameterList.Items.Add(item);
            }
        }

        private void frmSysParameter_Load(object sender, EventArgs e)
        {
            this.cmb_searchItem.SelectedIndex = 0;
            this.cmb_search_compare.SelectedIndex = 0;
            this.cmb_SysParamType.SelectedIndex = 0;
            this.BindListView(paramService.OperageSysParam().GetAllSysParam());
        }

        private void tsm_add_Click(object sender, EventArgs e)
        {
            tsb_add_Click(sender,e);
        }

        private void tsm_edit_Click(object sender, EventArgs e)
        {
            tsb_edit_Click(sender,e);
        }

        private void tsm_delete_Click(object sender, EventArgs e)
        {
            tsb_delete_Click(sender,e);
        }

        private void tsm_search_Click(object sender, EventArgs e)
        {
            tsb_search_Click(sender,e);
        }

        private void listview_SysParameterList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.listview_SysParameterList.SelectedItems.Count == 1)
            {
                actionString = "Edit";
                this.txt_SysParamName.Text = this.listview_SysParameterList.SelectedItems[0].SubItems[0].Text;
                this.cmb_SysParamType.SelectedText = this.listview_SysParameterList.SelectedItems[0].SubItems[1].Text;
                this.txt_SysParamValue.Text = this.listview_SysParameterList.SelectedItems[0].SubItems[2].Text;
                this.txt_SysParamRemark.Text = this.listview_SysParameterList.SelectedItems[0].SubItems[3].Text;
                this.txt_SysParamName.Enabled = false;
            }            
        }

       
    }
}
